﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel;
using Db;

namespace yq.Service
{
    /// <summary>
    /// 部门
    /// </summary>
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class Department : yq.IService.IDepartment
    {

        /// <summary>
        /// 获取全部部门
        /// </summary>
        /// <returns></returns>
        public IList<Model.Logic.DepartmentList> DepartmentAll()
        {
            return new yq.Business.DepartmentListDal().GetAllList(new Db.DbHelper());
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> AddDepartment(Model.Logic.DepartmentList model)
        {
            UpdatePath(model);

            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                new yq.Business.DepartmentListDal().Add(model, cn);
                cn.Commit();
                rv.True(model.DepartmentId);

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 根据上级重新设置对象数据
        /// </summary>
        /// <param name="model"></param>
        public void UpdatePath(Model.Logic.DepartmentList model)
        {
            var sep = Model.Logic.DepartmentList.Load(model.SuperiorDep);
            model.PathCount = sep.PathCount + 1;
            model.Path = sep.Path + model.DepartmentId + "/";
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> EditDepartment(Model.Logic.DepartmentList model)
        {
            // UpdatePath(model);

            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                if (new yq.Business.DepartmentListDal().Update(model, cn) != 0)
                {

                    cn.Commit();
                    rv.True(model.DepartmentId);
                }
                else
                {
                    rv.False("修改失败");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> DelDepartment(string departmentId)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                var dal = new yq.Business.DepartmentListDal();
                var info = dal.GetModel(obj => obj.DepartmentId == departmentId, cn);

                if (info.PathCount == 0)
                {
                    rv.False("顶级根不能删除");
                    return rv;
                }

                if (dal.GetCount(obj => obj.Path.Like(departmentId) && obj.DepartmentId != departmentId, cn) != 0)
                {
                    rv.False("请先移动或者删除下级的部门");
                    return rv;
                }

                //移动用户
                MoveDepartment(departmentId, info.SuperiorDep,cn);

                if (dal.Delete(departmentId, cn))
                {
                    rv.True(departmentId);
                }
                else
                {
                    rv.False("删除失败");
                }

                cn.Commit();

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }


        /// <summary>
        /// 移动用户所在部门
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public void MoveDepartment(string departmentId, string toDepartmentId, DbHelper cn)
        {
            Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
            var dal = new yq.Business.UserInfoDal();
            var userList = dal.GetList(obj => obj.DepartmentId == departmentId, cn);

            foreach (var info in userList)
            {
                info.DepartmentId = toDepartmentId;
                dal.Update(info, cn);
            }
        }

    }
}
